package com.four.peoplemanage.brcards.service;

import com.four.peoplemanage.brcards.dao.BrcardsDao;
import com.four.peoplemanage.brcards.entity.Brcards;
import com.four.peoplemanage.cards.entity.Cards;
import com.four.peoplemanage.common.bean.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BrcardsService {
    @Autowired
    private BrcardsDao brcardsDao;

    //查看
    public Page<Brcards> getOnrePage(int pageNum, int pageSize
    ){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Brcards> list=brcardsDao.canBrcards(start,pageSize);
        //获取总条目数
        int total=brcardsDao.countBrcards();
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Brcards> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }


    //添加
    public boolean insert(Brcards brcards){
        if (brcards == null){
            return false;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataTimeString=sdf.format(new Date());
        brcards.setPickuptime(dataTimeString);
        return brcardsDao.addBrcard(brcards)>0;
    }

}
